<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AdminRole extends Model
{
    protected $fillable = [
        "name","desc"
    ];
    //角色和权限的关系
    public function permission()
    {
        return $this->belongsToMany("App\AdminPermission","admin_permission_role","role_id","permission_id")->withPivot(["role_id","permission_id"]);
    }

    //给角色分配权限
    public function assignPermission($permission)
    {
        return $this->permission()->save($permission);
    }

    //删除角色的某一权限
    public function delPermission($permission)
    {
        return $this->permission()->detach($permission);
    }
    //角色是否拥有某一权限
    public function hasPermission($permission)
    {
        return $this->permission->contains($permission);
    }

}
